<?php

/**
 * This is the model class for table "work_orders".
 *
 * The followings are the available columns in table 'work_orders':
 * @property integer $id
 * @property integer $organization_id
 * @property string $wo_number
 * @property integer $location_id
 * @property string $start_date
 * @property string $required_by
 * @property string $accumulated_cost
 */
class WorkOrder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return WorkOrder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'work_orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('organization_id, wo_number, location_id, start_date, required_by, accumulated_cost', 'required'),
			array('organization_id, location_id', 'numerical', 'integerOnly'=>true),
			array('wo_number, accumulated_cost', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, organization_id, wo_number, location_id, start_date, required_by, accumulated_cost', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'location' => array(self::BELONGS_TO, 'ManufacturingLocation', 'location_id'),
			'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'organization_id' => 'Organization',
			'wo_number' => 'Wo Number',
			'location_id' => 'Location',
			'start_date' => 'Start Date',
			'required_by' => 'Required By',
			'accumulated_cost' => 'Accumulated Cost',
		);
	}
	
	public function behaviors()
	{ 
		return array( 'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', ); 
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('organization_id',$this->organization_id);
		$criteria->compare('wo_number',$this->wo_number,true);
		$criteria->compare('location_id',$this->location_id);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('required_by',$this->required_by,true);
		$criteria->compare('accumulated_cost',$this->accumulated_cost,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}